package sk.datalan.similarity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import sk.datalan.core.Utils;

public final class ProcessManager {
	private List<Process> processes = new ArrayList<Process>( 10 );
	private ExecutorService executor;

	public ProcessManager() {
		this.executor = Executors.newFixedThreadPool( 2 ); //number of procesors which will measure similarity of RealEstates
		
		
		this.processes.add( new Process( "OneBedroomApartment" ) );
		// this.processes.add( new Process( "TwoBedroomApartment" ) );
		// this.processes.add( new Process( "ThreeBedroomApartment" ) );
		// this.processes.add( new Process( "FourBedroomApartment" ) );
		// this.processes.add( new Process( "FiveBedroomApartment" ) );
		// this.processes.add( new Process( "FamilyHouse" ) );
	}

	public void start() {
		long startTime = System.currentTimeMillis();

		//Spustime vsetky query. Ak nie je volne jadro, Process je postaveny do fronty a caka kym sa neuvolni jadro
		for ( Process r : this.processes ) {
			this.executor.execute( r );
		}

		this.executor.shutdown();
		try {
			this.executor.awaitTermination( Long.MAX_VALUE, TimeUnit.MILLISECONDS ); //caka na ukoncenie vsetkych threadov
			Individual.count = 0;
			Utils.log( "Integration finished in " + ( System.currentTimeMillis() - startTime ) + "ms" );

		} catch ( InterruptedException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
